<div id="wrap">
	<div class="title"><?php echo $self['title']?></div>
	<div class="main">
	<?php if($admin_data){?>
		<div class="add">
			<p class="tt">添加主版面</p>
			<input type="text" id="name"><button id="add">添加</button>
		</div>
	<?php }?>
		<div class="list">
			<p class="tt">主版面列表</p>
			<table class="banmian">
				<thead>
					<tr><th>排序</th><th>版面名称</th><th>管理员</th><th>操作</th></tr>
				</thead>
				<tbody id="tbody">
				<?php foreach($zone_list as $item){  
					$ismanager = false;
				?>
					<tr>
						<td>
						<?php if($item['id'] != 1){ ?>
							<input type="number" value="<?php echo $item['sort']?>" class="sort" min="0">
						<?php }else{ ?>
						<?php echo '固定';}?>
						</td>
						<td><?php echo $item['name']?></td>
						<td>
							<ul>
							<?php
							foreach($item['manager'] as $val){
							if($val['id'] == $user_data['id']){
								$ismanager = true;
						}
						 ?>
								<li><a target="_blank" href="/userinfo.php?uid=<?php echo $val['id']?>"><?php echo $val['user']?></a></li>
							<?php } ?>
							</ul>
						</td>
						<td>
						<?php if($admin_data || $ismanager){ ?>
							<a href="?p=6&q=1&id=<?php echo $item['id']?>">修改</a>
							<?php if($admin_data){ ?>
							<span class="btn" id="<?php echo $item['id']?>">删除</span>
							<?php }?>
						<?php }else{ ?>
							<span>无权限</span>
						<?php } ?>
						</td>
					</tr>
				<?php } 
					if($zone_list){?>
					<tr></tr>
					<tr>
						<td><button id="sort">排序</button></td>
					</tr>
				<?php } ?>
				</tbody>
			</table>
			
		</div>
	</div>
</div>
</body>
<script type="text/javascript">
  function ajax(url,fun){
  	let xmlhttp = new XMLHttpRequest();
  	xmlhttp.onreadystatechange = fun;
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
  }
  let sort = document.getElementById('sort');
  let sort2 = document.getElementsByClassName('sort');
  let add = document.getElementById('add');
  let tbody = document.getElementById('tbody');
 if(add){
  add.onclick = function(){
  	let name = document.getElementById('name');
  	let name_val = name.value.trim();
  	if(name_val.length == 0){
  	    name.focus();
  		return
  	}
  	ajax(`add.php?type=1&name=${name_val}`,function() {
	    if(this.readyState==4 && this.status==200) {
	    	let {res} = JSON.parse(this.response);
	      	if(res){
	      		window.location.reload();
	      	}else{
	      		alert('版面已经存在了！')
	      	}
	    }
  	});
  }
 }
  sort.onclick = function(){
  	let len = sort2.length;
  	let str = '';
  	for(let i = 0;i<len;i++){
  		if(i !== len-1){
			str += sort2[i].value+',';
  		}else{
  			str += sort2[i].value;
  		}
  	}
  	ajax(`ud.php?type=1&sort=${str}`,function() {
	    if(this.readyState==4 && this.status==200) {
	    	let {res} = JSON.parse(this.response);
	      	if(res){
	      		window.location.reload();
	      	}
	    }
  	});
  }
 tbody.onclick = function(e) {
 	let id = e.target.getAttribute('id');
 	if(!/^\d+$/.test(id)){
 		return
 	}
 	if(confirm('您确定要删除吗?')){
  
	  	ajax(`del.php?type=1&id=${id}`,function() {
		    if(this.readyState==4 && this.status==200) {
		    	let {res} = JSON.parse(this.response);
		      	if(res){
		      		window.location.reload();
		      	}else{
		      		alert('无法删除,因为该区版下存在版面');
		      	}
		    }
	  	});
  	}
 }
</script>
</html>